package cate.game.activity.specall;

import cate.common.table.activity.specall.row.SpeCallBaseRow;
import cate.common.table.activity.specall.row.SpeCallEnsureRow;
import cate.game.GameBody;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpeCallStatus {
    @NoteField(value = "总抽取次数")
    @JsonIgnore
    public int totalTimes;

    @NoteField(value = "累计未中稀有召唤次数")
    @JsonIgnore
    public int callRareTimes;

    @NoteField(value = "未中Up累计次数")
    public int callUpTimes;

    @NoteField(value = "钻石抽取次数")
    public int diamondTimes;

    @NoteField(value = "免费次数 <类型, 免费次数>")
    public Map<Integer, Integer> freeTimes;

    @NoteField(value = "总的up次数，给作弊用的")
    @JsonIgnore
    public int totalUpTimes;

    public void initialize(){
        if (freeTimes == null) {
            freeTimes = new HashMap<>();
        }
    }

    public void clear() {
        totalTimes = 0;
        callRareTimes = 0;
        callUpTimes = 0;
        diamondTimes = 0;
        totalUpTimes = 0;
    }

    public boolean checkM1Times(SpeCallBaseRow baseRow) {
        return baseRow.m1CallTimes >= diamondTimes + baseRow.callTimes;
    }

    public boolean checkFree(int type) {
        return freeTimes.getOrDefault(type, 0) >= 1;
    }

    public void useFreeTimes(int type){
        freeTimes.put(type, freeTimes.get(type) - 1);
    }

    public void useM1Times(int times) {
        diamondTimes += times;
    }

    /**
     * 检测是否up保底
     */
    public int getUpGroup(SpeCallBaseRow baseRow) {
        if (callUpTimes + 1 >= baseRow.ensureUpTimes || cheatMustCall(baseRow, callUpTimes + 1)) {
            return 1;
        }
        return 0;
    }

    public int getRareGroup(GameBody game, SpeCallBaseRow baseRow) {
        SpeCallEnsureRow ensureRow = game.table.speCall.ensure.get(baseRow.configId);
        if (ensureRow == null) {
            return 0;
        }
        if (callRareTimes + 1 >= ensureRow.ensureRareTimes) {
            return ensureRow.rareGroup;
        }
        return 0;
    }

    public void update(boolean isRare, boolean isUp){
        totalTimes++;
        callRareTimes = isRare ? 0 : callRareTimes + 1;
        callUpTimes = isUp ? 0 : callUpTimes + 1;
        totalUpTimes += isUp ? 1 : 0;
    }

    public void updateFreeTimes(List<SpeCallBaseRow> rows){
        if (CollectionUtils.isNotEmpty(rows)) {
            for (SpeCallBaseRow row : rows) {
                freeTimes.put(row.type, row.freeTimes);
            }
        }
    }

    public void onDaySpan(List<SpeCallBaseRow> rows){
        updateFreeTimes(rows);
        diamondTimes = 0;
    }

    public boolean cheatMustNotCall(SpeCallBaseRow baseRow){
        return totalUpTimes < 1 && callUpTimes < baseRow.notCallTimes;
    }

    public boolean cheatMustCall(SpeCallBaseRow baseRow, int callTimes){
        return totalUpTimes < 1 && callTimes == baseRow.mustCallTimes;
    }
}
